import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: yuyong
 * @date: 2018-11-12 11:41 AM
 * desc:
 */
public class IdUtil {

    public static void main(String[] args) {
        new Thread(() -> {
            Set<Long> ids = new HashSet<>(10000);
            for (int i = 0; i < 10000; i++) {
                Long id = getGuid();
                ids.add(id);
            }
            System.out.println(ids.size());
        }).start();
    }

    /**
     * 16位末尾的数字id
     */
    public static int Guid=1000;
    static Lock lock = new ReentrantLock();

    public static Long getGuid() {
        lock.lock();
        IdUtil.Guid+=1;
        //要是一段时间内的数据连过大会有重复的情况，所以做以下修改
        if(IdUtil.Guid>9999){
            IdUtil.Guid=1000;
        }
        int ran=IdUtil.Guid;
        lock.unlock();
        //获取时间戳
        long now = System.currentTimeMillis();
        String info=now+"";

        return Long.parseLong(info.substring(2)+ran);
    }

    public static String getStrId() {
        Long guid = getGuid();
        return guid + "";
    }
}
